﻿/*
=======================================================
    UsingWireropeKeep【在用绳维保】
=======================================================
*/

namespace Services
{
    public interface IUsingWireropeKeepRepository : IRepository<UsingWireropeKeep>
    {
        UsingWireropeKeep GetLastKeepWirerope(int usingWireropeID);
    }


    public class UsingWireropeKeepRepository : BaseRepository<UsingWireropeKeep>, IUsingWireropeKeepRepository
    {
        public UsingWireropeKeepRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<UsingWireropeKeep> query)
            : base(dB, currentUser, query)
        {
        }

        #region 获取一套绳子最后一个维保数据
        public UsingWireropeKeep GetLastKeepWirerope(int usingWireropeID)
        {
            return _query.GetQueryable().Where(x => x.UsingWireropeID == usingWireropeID)
                .OrderBy(x => x.DoDate, OrderByType.Desc)
                .First();
        }
        #endregion

        #region GetPage
        public override (List<UsingWireropeKeep> items, int totalCount) GetPage(Expressionable<UsingWireropeKeep> where, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            var fs = _DB.Queryable<UsingWireropeKeep, WireropeKeepType>
               ((x, d) => new JoinQueryInfos(
                  JoinType.Inner, x.WireropeKeepTypeID == d.WireropeKeepTypeID
               ))
               .Select((x, d) => new UsingWireropeKeep
               {
                   UsingWireropeKeepID = x.UsingWireropeKeepID.SelectAll(),
                   WireropeKeepType_Name = d.Name
               });

            return _query.GetPage(where, pageIndex, pageSize, orderBy, fs);
        }
        #endregion

        #region GetModel
        public override UsingWireropeKeep GetModel(Expressionable<UsingWireropeKeep> where, string orderBy = null)
        {
            var fs = _DB.Queryable<UsingWireropeKeep, WireropeKeepType>
              ((x, d) => new JoinQueryInfos(
                 JoinType.Inner, x.WireropeKeepTypeID == d.WireropeKeepTypeID
              ))
              .Select((x, d) => new UsingWireropeKeep
              {
                  UsingWireropeKeepID = x.UsingWireropeKeepID.SelectAll(),
                  WireropeKeepType_Name = d.Name
              });

            return _query.GetModel(where, orderBy, fs);
        }
        #endregion

        #region GetSearch
        public override List<UsingWireropeKeep> GetSearch(Expressionable<UsingWireropeKeep> where, string orderBy = null, int? top = null)
        {
            var fs = _DB.Queryable<UsingWireropeKeep, WireropeKeepType>((x, wkt) => new JoinQueryInfos(
                JoinType.Inner, x.WireropeKeepTypeID == wkt.WireropeKeepTypeID
            )).Select((x, wkt) => new UsingWireropeKeep
            {
                UsingWireropeKeepID = x.UsingWireropeKeepID.SelectAll(),
                WireropeKeepType_Name = wkt.Name
            });

            return _query.GetSearch(where, orderBy, top, fs);
        }
        #endregion
    }
}
